

package net.jjjshop.shop.controller.user;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.jjjshop.common.entity.lbt.*;
import net.jjjshop.common.entity.user.User;
import net.jjjshop.common.entity.user.UserGrade;
import net.jjjshop.common.service.lbt.JybBusinessOrderService;
import net.jjjshop.common.service.lbt.JybTradeOrderService;
import net.jjjshop.common.service.lbt.LbSysConfigService;
import net.jjjshop.common.service.lbt.UserBillService;
import net.jjjshop.common.service.settings.RegionService;
import net.jjjshop.common.vo.RegionVo;
import net.jjjshop.framework.common.api.ApiResult;
import net.jjjshop.framework.log.annotation.OperationLog;
import net.jjjshop.shop.param.user.UserPageParam;
import net.jjjshop.shop.param.user.UserParam;
import net.jjjshop.shop.service.user.TagService;
import net.jjjshop.shop.service.user.UserGradeService;
import net.jjjshop.shop.service.user.UserService;
import net.jjjshop.shop.service.user.UserTagService;
import net.jjjshop.shop.vo.user.UserAgentVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Api(value = "user", tags = {"user"})
@RestController
@RequestMapping("/shop/user/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private UserGradeService userGradeService;
    @Autowired
    private TagService tagService;
    @Autowired
    private UserBillService billService;
    @Autowired
    private JybBusinessOrderService jybBusinessOrderService;
    @Autowired
    private JybTradeOrderService jybTradeOrderService;
    @Autowired
    private LbSysConfigService sysConfigService;

    @Autowired
    private RegionService regionService;

    @RequestMapping(value = "/index", method = RequestMethod.POST)
    @RequiresPermissions("/user/user/index")
    @OperationLog(name = "index")
    @ApiOperation(value = "index", response = String.class)
    public ApiResult<Map<String, Object>> index(@Validated @RequestBody UserPageParam userPageParam) throws Exception {
        Map<String, Object> result = new HashMap<>();
        List<UserGrade> gradeList = new ArrayList<>();
        UserGrade g = new UserGrade();
        g.setGradeId(1);
        g.setName("初级代理");
        UserGrade g0 = new UserGrade();
        g0.setGradeId(-1);
        g0.setName("普通会员");
        gradeList.add(g0);
        UserGrade g01 = new UserGrade();
        g01.setGradeId(0);
        g01.setName("VIP代理");
        gradeList.add(g01);

        gradeList.add(g);
        UserGrade g2 = new UserGrade();
        g2.setGradeId(2);
        g2.setName("中级代理");
        gradeList.add(g2);

        UserGrade g3 = new UserGrade();
        g3.setGradeId(3);
        g3.setName("高级代理");
        gradeList.add(g3);
        UserGrade g4 = new UserGrade();
        g4.setGradeId(4);
        g4.setName("总代理");
        gradeList.add(g4);
        UserGrade g5 = new UserGrade();
        g5.setGradeId(5);
        g5.setName("联合创始人");
        gradeList.add(g5);

        result.put("userList", userService.getList(userPageParam));
        result.put("gradeList",gradeList);
        result.put("tagList", tagService.getAll());
        return ApiResult.ok(result);
    }

    @RequestMapping(value = "/recharge", method = RequestMethod.POST)
    @RequiresPermissions("/user/user/recharge")
    @OperationLog(name = "recharge")
    @ApiOperation(value = "recharge", response = String.class)
    public ApiResult<Boolean> recharge(@Validated @RequestBody UserParam userParam) throws Exception {
        return ApiResult.ok(userService.recharge(userParam));
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @RequiresPermissions("/user/user/edit")
    @OperationLog(name = "edit")
    @ApiOperation(value = "edit", response = String.class)
    public ApiResult<Boolean> edit(@Validated @RequestBody UserParam userParam) throws Exception {
        UserGrade byId = userGradeService.getById(userParam.getGradeId());
        userParam.setLevel(byId.getWeight());

        return ApiResult.ok(userService.updateGrade(userParam));
    }

    @RequestMapping(value = "/toEditTag", method = RequestMethod.GET)
    @RequiresPermissions("/user/user/edit")
    @OperationLog(name = "toEditTag")
    @ApiOperation(value = "toEditTag", response = String.class)
    public ApiResult<Map<String, Object>> toEditTag(Integer userId) throws Exception {
        return ApiResult.ok(userService.toEditTag(userId));
    }


    @RequestMapping(value = "/editTag", method = RequestMethod.POST)
    @RequiresPermissions("/user/user/edit")
    @OperationLog(name = "editTag")
    @ApiOperation(value = "editTag", response = String.class)
    public ApiResult<String> toEditTag(@Validated @RequestBody UserParam userParam) throws Exception {
        if(userService.editTag(userParam)) {
            return ApiResult.ok(null, "修改成功");
        }else{
            return ApiResult.fail("修改失败");
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @RequiresPermissions("/user/user/delete")
    @OperationLog(name = "delete")
    @ApiOperation(value = "delete", response = String.class)
    public ApiResult<String> delete(Integer userId) throws Exception {
        if(userService.setDelete(userId)) {
            return ApiResult.ok(null, "删除成功");
        }else{
            return ApiResult.fail("删除失败");
        }
    }

    /**
     * 分页显示用户账单信息
     * @param request 搜索条件
     */
    //@PreAuthorize("hasAuthority('admin:member:userBillList')")
    @ApiOperation(value = "分页列表")
    @RequestMapping(value = "/userBillList", method = RequestMethod.GET)
    public ApiResult<IPage<UserBillEntityRequest>> userBillList(@ModelAttribute @Validated UserBillEntityRequest request,Integer page,Integer limit) {
        return ApiResult.ok( billService.userBillList(request, page,limit));
    }


    /**
     * 挂卖 回收分页列表
     */
    @ApiOperation(value = "挂卖 回收分页列表")
    @GetMapping("/getSaleList")
    public ApiResult<IPage<JybBusinessOrderEntity>> getSaleList(JybBusinessOrderRequest bean, Integer page, Integer limit){

        return ApiResult.ok(jybBusinessOrderService.getList(bean,page,limit));
    }

    /**
     * 列表信息
     */
    @ApiOperation(value = "列表信息")
    @GetMapping("/getTradeOrderList")
    public ApiResult<?> getTradeOrderList( Integer classType, Integer status, Integer page, Integer limit){
        return ApiResult.ok(jybTradeOrderService.selectOrder(null,null,status,classType,page,limit));
    }

    @ApiOperation(value = "取消交易订单")
    @RequestMapping(value = "/cancelOrder", method = RequestMethod.POST)
    public ApiResult<?> cancelOrder(@ModelAttribute @Validated TradeOrderEntityRequest request) {
        Boolean order = jybTradeOrderService.cancelOrder(request);
        if (order){
            return ApiResult.ok("操作成功");
        }
        return ApiResult.fail("操作失败");
    }


    /**
     * 获取系统配置
     */
    @ApiOperation(value = "获取系统配置")
        @RequestMapping(value = "/getSysConfig", method = RequestMethod.GET)
    public ApiResult<LbSysConfigEntity> getSysConfig(){
        return ApiResult.ok(sysConfigService.selectSysConfig());
    }

    /**
     * 获取系统配置
     */
    @ApiOperation(value = "获取系统配置")
    @RequestMapping(value = "/setSysConfig", method = RequestMethod.POST)
    public ApiResult<?> getSysConfig(LbSysConfigEntity config){
        LbSysConfigEntity lbSysConfigEntity = sysConfigService.selectSysConfig();
        if (null != lbSysConfigEntity){
            lbSysConfigEntity.setReleaseBl(config.getReleaseBl());
            lbSysConfigEntity.setTzMin(config.getTzMin());
            sysConfigService.updateById(lbSysConfigEntity);
        }else{
            sysConfigService.save(config);
        }
        return ApiResult.ok();
    }




    @ApiOperation(value = "设置用户代理")
    @RequestMapping(value = "/userAgent", method = RequestMethod.POST)
    public ApiResult<?> userAgent(@RequestBody UserAgentVo bean) {
        String s = userService.setUserRegin(bean.getUserId(), bean.getRegionId(),bean.getCityId(),bean.getProvinceId());
        if ("200".equals(s)){
            return ApiResult.ok("操作成功");
        }
        return ApiResult.fail(""+s);
    }

    @RequestMapping(value = "/getRegion", method = RequestMethod.POST)
    @OperationLog(name = "getRegion")
    @ApiOperation(value = "getRegion", response = String.class)
    public ApiResult<List<JSONArray>> getRegion() {
        return ApiResult.ok(regionService.getRegionForApi());
    }
    @RequestMapping(value = "/getArea", method = RequestMethod.POST)
    @OperationLog(name = "getArea")
    @ApiOperation(value = "getArea", response = String.class)
    public ApiResult<List<RegionVo>> getArea() {
        List<RegionVo> area = regionService.getArea();
        return ApiResult.ok(area);
    }

}
